package com.easondongh.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easondongh.business.domain.Provider;
import com.easondongh.business.mapper.ProviderMapper;
import com.easondongh.business.vo.GoodsQueryVo;
import com.easondongh.business.vo.ProviderQueryVo;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.common.vo.PageResult;
import com.easondongh.system.constants.Constants;
import com.easondongh.system.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easondongh.business.domain.Goods;
import com.easondongh.business.mapper.GoodsMapper;
import com.easondongh.business.service.GoodsService;
import org.springframework.util.CollectionUtils;

/**
 * @author EasonDongH
 * @date 2020/6/1 10:51
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService{

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private ProviderMapper providerMapper;

    @Override
    public PageResult<Goods> getByPage(BasePageQueryVo<GoodsQueryVo> queryCondition) {
        IPage<Goods> page = new Page<>(queryCondition.getPage(), queryCondition.getLimit());
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        GoodsQueryVo queryParams = queryCondition.getQueryParams();
        if (queryParams != null) {
            queryWrapper.eq(queryParams.getProviderId() != null, Goods.COL_PROVIDER_ID, queryParams.getProviderId());
            queryWrapper.like(StringUtils.isNotBlank(queryParams.getGoodsName()), Goods.COL_GOODS_NAME, queryParams.getGoodsName());
            queryWrapper.like(StringUtils.isNotBlank(queryParams.getBatchNo()), Goods.COL_BATCH_NO, queryParams.getBatchNo());
            queryWrapper.like(StringUtils.isNotBlank(queryParams.getPromitCode()), Goods.COL_PROMIT_CODE, queryParams.getPromitCode());
            queryWrapper.like(StringUtils.isNotBlank(queryParams.getSpec()), Goods.COL_SPEC, queryParams.getSpec());
        }
        queryWrapper.eq(Goods.COL_ENABLE, Constants.ENABLE);
        IPage<Goods> iPage = this.goodsMapper.selectPage(page, queryWrapper);
        List<Goods> goodsList = iPage.getRecords();
        if(!CollectionUtils.isEmpty(goodsList)) {
            List<Provider> providerList = this.providerMapper.getAllEnableProvider();
            if(!CollectionUtils.isEmpty(providerList)) {
                Map<Integer, String> collect = providerList.stream().collect(Collectors.toMap(Provider::getId, Provider::getProviderName));
                for (Goods goods : goodsList) {
                    goods.setProviderName(collect.get(goods.getProviderId()));
                }
            }
        }
        return new PageResult<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), goodsList);
    }

    @Override
    public boolean enableById(Integer id) {
        Goods goods = new Goods();
        goods.setId(id);
        goods.setEnable(Constants.ENABLE);
        return this.goodsMapper.updateById(goods) == 1;
    }

    @Override
    public boolean disableById(Integer id) {
        Goods goods = new Goods();
        goods.setId(id);
        goods.setEnable(Constants.DISABLE);
        return this.goodsMapper.updateById(goods) == 1;
    }

    @Override
    public List<Goods> getAllEnableGoods() {
        return this.goodsMapper.getAllEnableGoods();
    }

    /**
     * 更新库存，num为整数为增加 负数为减少  已保证数据库该值最小值为0
     * @param goodsId
     * @param num
     * @return
     */
    @Override
    public boolean updateStock(Integer goodsId, int num) {
        if(goodsId == null) {
            return  false;
        }
        if(num == 0) {
            return  true;
        }
        int update = this.goodsMapper.updateStock(goodsId, num);
        return update == 1;
    }
}
